﻿using Furion;
using Furion.DataEncryption;
using Furion.DynamicApiController;
using FurionApi.Demo.Core.Const;
using FurionApi.Demo.Core.Options;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FurionApi.Demo.Application.System.Auth
{

    /// <summary>
    /// 认证
    /// </summary>
    [ApiDescriptionSettings(KeepVerb = true, SplitCamelCase = false)]
    public class AuthAppService : IDynamicApiController
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public AuthAppService(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        [AllowAnonymous]
        public string Login(string username, string password)
        {
            var accessToken = JWTEncryption.Encrypt(new Dictionary<string, object>
            {
                {ClaimConst.CLAINM_USERID, 1},
                {ClaimConst.CLAINM_ACCOUNT, "test"},
                {ClaimConst.CLAINM_NAME, "测试用户"},
                {ClaimConst.CLAINM_SUPERADMIN, 1},
                { ClaimConst.CLAINM_LEVAL, 0}
            });

            // 设置Swagger自动登录
            _httpContextAccessor.HttpContext.SigninToSwagger(accessToken);

            // 生成刷新Token令牌
            var refreshToken =
                JWTEncryption.GenerateRefreshToken(accessToken, App.GetOptions<RefreshTokenSettingOptions>().ExpiredTime);

            // 设置刷新Token令牌
            _httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
            return accessToken;
        }
    }
}
